package com.zretc.controller;

import com.zretc.entity.Permission;
import com.zretc.entity.ResponseResult;
import com.zretc.entity.Role;
import com.zretc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 周文涛
 * 2020年8月24日13:36:15
 * 角色管理
 */
@RestController
@RequestMapping("/user/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    // 添加角色
    @PostMapping("/add")
    public ResponseResult addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }
    // 删除角色
    @DeleteMapping("/{id}")
    public ResponseResult deleteRole(@PathVariable("id")Integer roleId){
        return roleService.deleteRole(roleId);
    }
    // 更新角色
    @PutMapping("/")
    public ResponseResult updateRole(@RequestParam("roleId") Integer roleId,
                                     @RequestParam("permissionId")Integer[] permissionId){
        return roleService.updateRole(roleId,permissionId);
    }
    // 查询全部权限
    @GetMapping("/")
    public List<Role> findAllRole(){
        List<Role> roles = roleService.finAll();
        return roles;
    }
    // 查询全部权限
    @GetMapping("/permission")
    public List<Permission> findAllPermission(){
        List<Permission> list = roleService.finAllPermission();
        return list;
    }

    // 根据角色编号查询权限
    @GetMapping("/permission/{id}")
    public List<Integer> findPermissionByRoleId(@PathVariable("id")Integer roleId){
        return roleService.findPermissionIdByRoleId(roleId);
    }
}
